#!/bin/bash

#    / **  
#    * 文件名：安装linux软件
#    * 版权： Copyright 谢振斌  All Rights Reserved.  
#    * 修改内容：新增  
#    * 修改人： 谢振斌  
#    * 修改时间：2021-3-23  
#    */  

#    / **  
#    *〈一句话功能简述〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function fun1()
{  
	echo "helld world !"

	# 在shell中：
	# $0: 获取当前脚本的名称
	# $#: 传递给脚本的参数个数
	# $$: shell脚本的进程号
	# $1, $2, $3...：脚本程序的参数
}


#    / **  
#    *〈获得发行版本名称〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function getDistName()
{
	if grep -Eqii "Arch Linux" /etc/issue || grep -Eq "Arch Linux" /etc/*-release; then
        DISTRO='Arch Linux'
        PM='pacman'
    elif grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
    elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        PM='yum'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        PM='yum'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        PM='yum'
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        PM='apt'
    elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        DISTRO='Raspbian'
        PM='apt'
    else
        DISTRO='unknow'
    fi

    # 返回值
    echo $DISTRO;
}

#    / **  
#    *〈 安装软件 〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [软件名称]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function installSoftware()
{
    echo "[INFO] 安装软件"
    softwareName=$1
    echo "[Debug] softwareName="${softwareName}
    # 判断是否已经安装
    cmdStr="which "${softwareName}
    if [ -x "$(cmdStr)" ]; then
        # 获取当前系统发行版本
        systemType=$(getDistName)
        echo "[Debug] systemType="${systemType}
        # 命令拼接
        #cmdStr="ln -s  "${dir}"/zshrc-oh-my-sh  "${HOME}"/.zshrc"
        #cmdStr="git clone "${URL}${pluginsName}" "${pluginsPath}
        #echo "执行命令: ${cmdStr}"
        #${cmdStr}
        if [ "$systemType" == "CentOS" ]; then
            echo "[Debug] ======= CentOS"
            # 执行另一个脚本
            # /bin/bash ./zsh1.sh  
            #cmdStr="ln -s  "${dir}"/zshrc-oh-my-sh  "${HOME}"/.zshrc"
            cmdStr="yum install "${softwareName}
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
        elif [ "$systemType" == "Arch Linux" ] ; then
            echo "[Debug] ======= Arch Linux"
            cmdStr="yay -S "${softwareName}
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
        else
            echo "[Debug] ======= unknow"
            echo "[INFO] 不知道当前系统是什么，不知到该用什么包管理软件进行安装！软件${systemType}"
        fi
      if [ ! $? -eq 0 ]; then
        echo ""
        echo "[ERROR]: 安装软件${softwareName}  失败 !!"
        exit
      fi
    else
        echo "[INFO] 软件${softwareName}已经存在！"
    fi
}

#    / **  
#    *〈 安装软件 从git仓库〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [git仓库地址]  
#    * @param [参数2] [本地存放地址]  
#    * @param [参数3] [克隆后执行的安装命令] 
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function installPluginsFormGit()
{
    echo "[INFO] 开始 从git仓库安装软件 "
    pluginsName=$1
    echo "[Debug] pluginsName="${pluginsName}
    pluginsPath=$2
    echo "[Debug] pluginsPath="${pluginsPath}
    echo "[INFO] 安装插件："${pluginsName}
    executableInstallCmdStr=$3
    echo "[Debug] executableInstallCmdStr="${executableInstallCmdStr}

    # 判断是否路径是否已经存在
    if [ ! -d "$pluginsPath" ]; then
      echo "Installing "${pluginsName}
      #URL="https://github.com/zsh-users/"
      #URL="https://hub.fastgit.org/"
      URL="https://ghproxy.com/https://github.com/"
      if [ -x "$(which git)" ]; then
        # 命令拼接
        #cmdStr="ln -s  "${dir}"/zshrc-oh-my-sh  "${HOME}"/.zshrc"
        cmdStr="git clone "${URL}${pluginsName}" "${pluginsPath}
        echo "执行命令: ${cmdStr}"
        ${cmdStr}
        echo "[INFO] 安装插件"${pluginsName}"安装完成！"

      else
        echo "[ERROR]: 请先安装 git !!"
        exit
      fi
      if [ ! $? -eq 0 ]; then
        echo ""
        echo "[ERROR]: 克隆${pluginsName}仓库中出错!!"
        exit
      fi
    else
        echo "[INFO] ${pluginsName}已经存在！"
    fi
}

#    / **  
#    *〈流程控制〉  
#    *〈该脚本的主函数〉  
#    * @param [无]  
#    * @return [无]  
#    * @exception/throws [无]  
#    * @deprecated  
#    */  
function processControlMain()  
{  
    # 打印当前系统时间  
    echo "Start..."  
    date  

    # 此处是你的代码  
    # ...  
    # fun1

    # 安装vim可搭配的第三方软件
    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'liuchengxu/vim-which-key' ${pluginsPath}"/vim-which-key";

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'vim-ctrlspace/vim-ctrlspace' ${pluginsPath}"/vim-ctrlspace";

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'mhinz/vim-startify' ${pluginsPath}'/vim-startify';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'airblade/vim-rooter' ${pluginsPath}'/vim-rooter';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'francoiscabrol/ranger.vim' ${pluginsPath}'/ranger.vim';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'mhinz/vim-signify' ${pluginsPath}'/vim-signify';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'iberianpig/tig-explorer.vim' ${pluginsPath}'/tig-explorer.vim';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'rbgrouleff/bclose.vim' ${pluginsPath}'/bclose.vim';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'junegunn/fzf' ${pluginsPath}'/fzf';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'junegunn/fzf.vim' ${pluginsPath}'/fzf.vim';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'mbbill/undotree' ${pluginsPath}'/undotree';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'tpope/vim-surround' ${pluginsPath}'/vim-surround';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'gcmt/wildfire.vim' ${pluginsPath}'/wildfire.vim';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'dhruvasagar/vim-table-mode' ${pluginsPath}'/vim-table-mode';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'SirVer/ultisnips' ${pluginsPath}'/ultisnips';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'honza/vim-snippets' ${pluginsPath}'/vim-snippets';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'neoclide/coc.nvim' ${pluginsPath}'/coc.nvim' 'ls';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'christoomey/vim-tmux-navigator' ${pluginsPath}'/vim-tmux-navigator';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'vim-scripts/fcitx.vim' ${pluginsPath}'/fcitx.vim';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit '520Matches/fcitx5.vim' ${pluginsPath}'/fcitx5.vim';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'akinsho/nvim-bufferline.lua' ${pluginsPath}'/nvim-bufferline.lua';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'vim-airline/vim-airline' ${pluginsPath}'/vim-startify';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'vim-airline/vim-airline-themes' ${pluginsPath}'/vim-airline-themes';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'tomasr/molokai' ${pluginsPath}'/molokai';

    pluginsPath=${HOME}'/.vim/plugins';
    installPluginsFormGit 'yuttie/inkstained-vim' ${pluginsPath}'/inkstained-vim';


	# 执行另一个脚本
	# /bin/bash ./zsh2.sh
    # 打印当前系统时间  
    date  
    echo "The End."  
}  

# 执行入口 
# 进入脚本目录位置
cd `dirname $0`
# 获取当前脚本目录位置
shellfilepath=`pwd`'/'$0
# 打印脚本目录地址
echo "[INFO]: 脚本地址 "${shellfilepath}

if true  
# if false  
then  
    echo "[Debug] true"
    # 开始执行  从标准输入中读取并同时写入到标准输出和指定的文件上 
    ## 拼接日志文件地址
    #logfilepath=`pwd`'/'$0'.log'
    logfilepath=`pwd`'/'$0'-'$(date +%Y%m%d-%H-%M-%S)'.log'
    #logfilepath=`pwd`'/'$0'-'$(date +%Y%m%d-%H-%M-%S)'.log'
    ## 打印日志文件地址
    echo "[INFO]: 日志地址" ${logfilepath}
    processControlMain  | tee -a ${logfilepath}
else  
    echo "[Debug] false"  
    ## 函数单元测试  
    #  
fi  

# 脚本结束  


